<template>
  <div>
    <p>远程搜索</p>
    <el-select
      v-model="value"
      multiple
      filterable
      remote
      reserve-keyword
      placeholder="请输入关键词"
      :remote-method="remoteMethod"
      :loading="loading"
    >
      <el-option
        v-for="item in options"
        :key="item.value"
        :label="item.label"
        :value="item.value"
      >
      </el-option>
    </el-select>
    {{ value }}
  </div>
</template>

<script>
import { getSelectData } from "@/api/service";
export default {
  data() {
    return {
      options: [],
      value: [],
      list: [],
      loading: false,
      states: [] /*改为远程数据*/,
    };
  },
  mounted() {
    this.list = this.states.map((item) => {
      return { value: `value:${item}`, label: `label:${item}` };
    });
  },
  methods: {
    remoteMethod(query) {
      if (query !== "") {
        this.loading = true;
        // setTimeout(() => {
        //   this.loading = false;
        //   this.options = this.list.filter((item) => {
        //     return item.label.toLowerCase().indexOf(query.toLowerCase()) > -1;
        //   });
        // }, 200);
        getSelectData(query).then((data) => {
          this.loading = false;
          this.options = data.map((item) => {
            return { value: `value:${item}`, label: `label:${item}` };
          });
        });
      } else {
        this.options = [];
      }
    },
  },
};
</script>